package com.jiyongjun.component.core.model;

import com.jiyongjun.component.enums.LogConstant;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * <h2></h2>
 * <h3>描述：</h3>
 *
 * @author yongjun.jyj
 * @since 2022/7/14 19:20
 */
@Getter
@Slf4j
public class CustomLogContext extends BaseLogContext<CustomLogContext>{

    public static final CustomLogContext NO_OP_CONTEXT = new CustomLogContext(null, null);
    /**
     * @param apiUniqueName 接口名
     * @param logThreadLocal 日志上下文
     */
    protected CustomLogContext(String apiUniqueName, ThreadLocal<Map<String, BaseLogContext<?>>> logThreadLocal) {
        super(apiUniqueName, logThreadLocal);
    }

    public @NotNull static CustomLogContext getLogContext(String apiName) {
        return getLogContext(apiName, true);
    }

    public @NotNull static CustomLogContext getLogContext(String apiName, boolean createIfAbsent) {
        try {
            return (CustomLogContext) LogContext.getLogContext(CustomLogContext.class, apiName, createIfAbsent);
        } catch (Exception e) {
            log.warn("{}|获取custom日志上下文异常|apiName={},msg={}", LogConstant.LOG_EXCEPTION_KEYWORD, apiName, e.getMessage(), e);
            return NO_OP_CONTEXT;
        }
    }


}
